
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */


#include "main.h"
#include "adc.h"
#include "usart.h"
#include "rtc.h"
#include "tim.h"
#include "gpio.h"




#include "string.h"
#include "EC200T.h"
#include "CH9120.h"
#include "flash.h"





unsigned char UART1_Rx_Buf[MAX_REC_LENGTH] = {0};                
unsigned int  UART1_Rx_cnt = 0;                 
unsigned char UART1_temp[REC_LENGTH] = {0}; 

unsigned char UART2_Rx_Buf[MAX_REC_LENGTH] = {0};             
unsigned int  UART2_Rx_cnt = 0;                 
unsigned char UART2_temp[REC_LENGTH] = {0}; 

unsigned char LPUART1_Rx_Buf[MAX_REC_LENGTH] = {0};             
unsigned int  LPUART1_Rx_cnt = 0;                 
unsigned char LPUART1_temp[REC_LENGTH] = {0};

extern uint8_t CH9120_Mode;
extern uint8_t CH9120_DHCP ;

extern uint8_t CH9120_Local_IP[4]; 
extern uint8_t CH9120_Local_GW[4];
extern uint8_t CH9120_Local_MASK[4];

uint8_t Recv_CH9120_Local_IP[4]={0x00,0x00,0x00,0x00}; 
uint8_t Recv_CH9120_Local_GW[4]={0x00,0x00,0x00,0x00}; 
uint8_t Recv_CH9120_Local_MASK[4]={0x00,0x00,0x00,0x00}; 






#define VERSION "Remote-Control-HW1.2 v1.16-20220420"
#define VREF_REG		*((uint16_t*)(0x1FF80078))

uint8_t hw_version = '1';
uint8_t sw_version = '1';

uint32_t timer_cnt = 0;
uint8_t adc_sample_time = 5;

uint8_t TBOX_RSSI;

uint8_t Sys_Device_ID[25];

uint8_t ETH0_config = 0x00; 


uint8_t CH9120_Dest_IP[4] = {119,3,32,99}; 
uint16_t CH9120_Dest_PORT = 10011;











uint8_t Dest_IP[16]={'l','p','w','a','n','.','i','o','t','.','s','t','o','r','e',0}; 
uint8_t Dest_PORT[6]={'1','0','0','1','1',0}; 













uint8_t Tx_Err_Cnt = 0;
uint8_t Feedback_Err_Cnt = 0;
uint8_t HeartBeat_Packet[100]={'2','3','2','3','5','1','4','8'}; 

uint8_t Keep_Link_Packet[40]={'2','3','2','3','5','1','1','F'}; 

uint8_t MCU_IO_IN_Val = 0x00;

uint16_t adc_channel0_low_th = 4320; 
uint16_t adc_channel0_high_th = 4800; 
uint16_t adc_channel1_low_th = 5600; 
uint16_t adc_channel1_high_th = 7200; 
uint8_t adc_channel0_status = 0x00; 
uint8_t adc_channel1_status = 0x00; 
uint8_t adc_channel0_status_bf = 0x00; 
uint8_t adc_channel1_status_bf = 0x00; 
uint16_t adc_channel0_val; 
uint16_t adc_channel1_val; 


uint8_t backup_pwr = 0;
uint8_t backup_pwr_bf = 0;
uint16_t adc_channel5_val; 
uint16_t adc_channel5_low_th = 7000; 
uint16_t adc_channel5_high_th = 10000; 

uint8_t recv_server_cmd = 0; 

uint8_t Set_Rely_ascii = '0';
uint8_t Reset_Rely_ascii = 'f';

uint8_t Delay_Rely_ascii = '0';
uint8_t Rely1_Delay_flag = 0;
uint8_t Rely2_Delay_flag = 0;
uint8_t Rely3_Delay_flag = 0;
uint8_t Rely4_Delay_flag = 0;
uint32_t Rely1_Delay_Tick;
uint32_t Rely2_Delay_Tick;
uint32_t Rely3_Delay_Tick;
uint32_t Rely4_Delay_Tick;
uint32_t Time_Delay_Tick = 10000; 

uint8_t Rely1_status = '0';
uint8_t Rely2_status = '0';
uint8_t Rely3_status = '0';
uint8_t Rely4_status = '0';
uint8_t Rely1_status_bf = '0';
uint8_t Rely2_status_bf = '0';
uint8_t Rely3_status_bf = '0';
uint8_t Rely4_status_bf = '0';

uint32_t Period_Tick;
uint32_t Keep_link_Tick;
uint32_t Sample_adc_Tick;
uint32_t Sent_Pack_Tick;


uint32_t Sample_adc_Period = 1000; 
uint32_t Time_Period = 1800000; 

uint32_t Keep_Link_Period = 15000; 
uint32_t Reboot_Time_Period = 86400000; 

uint8_t Pack_cnt = 0;


uint8_t Config_Data[32];

uint8_t EC200T_Tx_Flag = 0x00;
uint8_t Eth0_Tx_Flag = 0x00;



















void read_sys_config(void)
{
	uint32_t DevID;
	uint8_t Device_ID[12];
	uint8_t i;
	DevID = HAL_GetUIDw0();
	Device_ID[11]=DevID;
	Device_ID[10]=DevID>>8;
	Device_ID[9]=DevID>>16;
	Device_ID[8]=DevID>>24;
	DevID = HAL_GetUIDw1();
	Device_ID[7]=DevID;
	Device_ID[6]=DevID>>8;
	Device_ID[5]=DevID>>16;
	Device_ID[4]=DevID>>24;
	DevID = HAL_GetUIDw2();
	Device_ID[3]=DevID;
	Device_ID[2]=DevID>>8;
	Device_ID[1]=DevID>>16;
	Device_ID[0]=DevID>>24;
	for(i=0;i<12;i++)
	{
		HeartBeat_Packet[9+i*2] = lowhextoascii(Device_ID[11-i]>>4);
		HeartBeat_Packet[9+i*2+1] = lowhextoascii(Device_ID[11-i]);
		Sys_Device_ID[0+i*2] = HeartBeat_Packet[9+i*2];
		Sys_Device_ID[1+i*2] = HeartBeat_Packet[9+i*2+1];
	}
	Sys_Device_ID[24] = 0;
	SysPrintf("device-id=%s\r\n",Sys_Device_ID);
	for(i=0;i<20;i++) 
	{
		HeartBeat_Packet[33+i] = '0';
	}
	for(i=0;i<24;i++)
		Keep_Link_Packet[9+i] = HeartBeat_Packet[9+i];
}


void Pack_packet(void)
{
	HeartBeat_Packet[6] = '4'; 
	HeartBeat_Packet[7] = '8'; 
	HeartBeat_Packet[53] = backup_pwr + '0'; 
	HeartBeat_Packet[54] = lowhextoascii(adc_channel5_val>>12);	
	HeartBeat_Packet[55] = lowhextoascii(adc_channel5_val>>8);	
	HeartBeat_Packet[56] = lowhextoascii(adc_channel5_val>>4);	
	HeartBeat_Packet[57] = lowhextoascii(adc_channel5_val);
	HeartBeat_Packet[58] = lowhextoascii(adc_channel0_val>>12);	
	HeartBeat_Packet[59] = lowhextoascii(adc_channel0_val>>8);	
	HeartBeat_Packet[60] = lowhextoascii(adc_channel0_val>>4);	
	HeartBeat_Packet[61] = lowhextoascii(adc_channel0_val);
	HeartBeat_Packet[62] = lowhextoascii(adc_channel1_val>>12);	
	HeartBeat_Packet[63] = lowhextoascii(adc_channel1_val>>8);	
	HeartBeat_Packet[64] = lowhextoascii(adc_channel1_val>>4);	
	HeartBeat_Packet[65] = lowhextoascii(adc_channel1_val);
	HeartBeat_Packet[66] = Rely4_status;
	HeartBeat_Packet[67] = Rely3_status;
	HeartBeat_Packet[68] = Rely2_status;
	HeartBeat_Packet[69] = Rely1_status;
	HeartBeat_Packet[70] = ((MCU_IO_IN_Val&0x08)>>3) + '0';
	HeartBeat_Packet[71] = ((MCU_IO_IN_Val&0x04)>>2) + '0';
	HeartBeat_Packet[72] = ((MCU_IO_IN_Val&0x02)>>1) + '0';
	HeartBeat_Packet[73] = (MCU_IO_IN_Val&0x01) + '0';
	HeartBeat_Packet[74] = lowhextoascii(TBOX_RSSI>>4);
	HeartBeat_Packet[75] = lowhextoascii(TBOX_RSSI);
	HeartBeat_Packet[76] = lowhextoascii(Pack_cnt>>4);
	HeartBeat_Packet[77] = lowhextoascii(Pack_cnt);
	HeartBeat_Packet[78] = 'A';
	HeartBeat_Packet[79] = 'A';
	HeartBeat_Packet[80] = 'A';
	HeartBeat_Packet[81] = 'A';
}


char lowhextoascii(char hexdata)
{
	char temp;
	temp=hexdata&0x0f;
	if(temp<10)
		temp=temp+0x30;
	else
		temp=temp+0x37;
	return temp;
}

uint8_t Wait_Local_Config_cmd(void)
{

	
	
	
	
	
	
	
	
	uint16_t i,j,k;
	uint16_t ip_begin,ip_end,port_begin,port_end,end_pos,pos;;
	uint8_t m,n,dot_cnt;
	uint8_t dot_pos[3] = {0x00,0x00,0x00};
	
	m = 3;
	dot_cnt = 0;
	SysPrintf("len=%d\r\n",UART2_Rx_cnt);
	if(UART2_Rx_cnt>=47) 
	{
		UART2_Rx_Buf[UART2_Rx_cnt] = 0;
		SysPrintf("len=%d\r\n",UART2_Rx_cnt);
		SysPrintf("local Recv:%s\r\n",UART2_Rx_Buf);	
		if( (strstr(UART2_Rx_Buf,"setip=")!=NULL)  && (strstr(UART2_Rx_Buf,"setgw=")!=NULL) && (strstr(UART2_Rx_Buf,"setmask=")!=NULL))
		{
			
			for(i=0;i<UART2_Rx_cnt-12;i++)
			{
				if( (UART2_Rx_Buf[i]=='i') && (UART2_Rx_Buf[i+1]=='p') && (UART2_Rx_Buf[i+2]=='=') )  
				{
					for(j=i+3;j<UART2_Rx_cnt-1;j++)
					{
						if((UART2_Rx_Buf[j]=='\r') && (UART2_Rx_Buf[j+1]=='\n'))
						{
							end_pos = j;
							break;
						}
					}
					
					for(j=i+3;j<end_pos;j++)
					{
						if(UART2_Rx_Buf[j]=='.')
						{
							dot_pos[dot_cnt] = j;
							dot_cnt++;
							if(dot_cnt==3)
								break;
						}
					}
					
					
					
					if(dot_cnt!=3) 
					{
						SysPrintf("err03: recv error ip format\r\n");
						UART2_Rx_cnt = 0;
						return 0;
					}
					else
					{
						for(j=i+3;j<dot_pos[0];j++)
						{
							Recv_CH9120_Local_IP[0] = Recv_CH9120_Local_IP[0]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[0]+1;j<dot_pos[1];j++)
						{
							Recv_CH9120_Local_IP[1] = Recv_CH9120_Local_IP[1]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[1]+1;j<dot_pos[2];j++)
						{
							Recv_CH9120_Local_IP[2] = Recv_CH9120_Local_IP[2]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[2]+1;j<end_pos;j++)
						{
							Recv_CH9120_Local_IP[3] = Recv_CH9120_Local_IP[3]*10 + UART2_Rx_Buf[j]-'0';
						}
						
						SysPrintf("recv source ip:%d.%d.%d.%d\r\n",Recv_CH9120_Local_IP[0],Recv_CH9120_Local_IP[1],Recv_CH9120_Local_IP[2],Recv_CH9120_Local_IP[3]);
						
						break; 
					}						
				}
			}
			
			dot_cnt = 0x00;
			dot_pos[0] = 0x00;
			dot_pos[1] = 0x00;
			dot_pos[2] = 0x00;
			for(i=0;i<UART2_Rx_cnt-12;i++)
			{
				if( (UART2_Rx_Buf[i]=='g') && (UART2_Rx_Buf[i+1]=='w') && (UART2_Rx_Buf[i+2]=='=') )  
				{
					for(j=i+3;j<UART2_Rx_cnt-1;j++)
					{
						if((UART2_Rx_Buf[j]=='\r') && (UART2_Rx_Buf[j+1]=='\n'))
						{
							end_pos = j;
							break;
						}
					}
					
					for(j=i+3;j<end_pos;j++)
					{
						if(UART2_Rx_Buf[j]=='.')
						{
							dot_pos[dot_cnt] = j;
							dot_cnt++;
							if(dot_cnt==3)
								break;
						}
					}
					
					
					
					if(dot_cnt!=3) 
					{
						SysPrintf("err03: recv error gw format\r\n");
						UART2_Rx_cnt = 0;
						return 0;
					}
					else
					{
						for(j=i+3;j<dot_pos[0];j++)
						{
							Recv_CH9120_Local_GW[0] = Recv_CH9120_Local_GW[0]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[0]+1;j<dot_pos[1];j++)
						{
							Recv_CH9120_Local_GW[1] = Recv_CH9120_Local_GW[1]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[1]+1;j<dot_pos[2];j++)
						{
							Recv_CH9120_Local_GW[2] = Recv_CH9120_Local_GW[2]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[2]+1;j<end_pos;j++)
						{
							Recv_CH9120_Local_GW[3] = Recv_CH9120_Local_GW[3]*10 + UART2_Rx_Buf[j]-'0';
						}
						
						SysPrintf("recv source gw:%d.%d.%d.%d\r\n",Recv_CH9120_Local_GW[0],Recv_CH9120_Local_GW[1],Recv_CH9120_Local_GW[2],Recv_CH9120_Local_GW[3]);
						
						break; 
					}						
				}
			}
			
			dot_cnt = 0x00;
			dot_pos[0] = 0x00;
			dot_pos[1] = 0x00;
			dot_pos[2] = 0x00;
			for(i=0;i<UART2_Rx_cnt-12;i++)
			{
				if( (UART2_Rx_Buf[i]=='s') && (UART2_Rx_Buf[i+1]=='k') && (UART2_Rx_Buf[i+2]=='=') )  
				{
					for(j=i+3;j<UART2_Rx_cnt-1;j++)
					{
						if((UART2_Rx_Buf[j]=='\r') && (UART2_Rx_Buf[j+1]=='\n'))
						{
							end_pos = j;
							break;
						}
					}
					
					for(j=i+3;j<end_pos;j++)
					{
						if(UART2_Rx_Buf[j]=='.')
						{
							dot_pos[dot_cnt] = j;
							dot_cnt++;
							if(dot_cnt==3)
								break;
						}
					}
					
					
					
					if(dot_cnt!=3) 
					{
						SysPrintf("err03: recv error mask format\r\n");
						UART2_Rx_cnt = 0;
						return 0;
					}
					else
					{
						for(j=i+3;j<dot_pos[0];j++)
						{
							Recv_CH9120_Local_MASK[0] = Recv_CH9120_Local_MASK[0]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[0]+1;j<dot_pos[1];j++)
						{
							Recv_CH9120_Local_MASK[1] = Recv_CH9120_Local_MASK[1]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[1]+1;j<dot_pos[2];j++)
						{
							Recv_CH9120_Local_MASK[2] = Recv_CH9120_Local_MASK[2]*10 + UART2_Rx_Buf[j]-'0';
						}
						for(j=dot_pos[2]+1;j<end_pos;j++)
						{
							Recv_CH9120_Local_MASK[3] = Recv_CH9120_Local_MASK[3]*10 + UART2_Rx_Buf[j]-'0';
						}
						
						SysPrintf("recv source mask:%d.%d.%d.%d\r\n",Recv_CH9120_Local_MASK[0],Recv_CH9120_Local_MASK[1],Recv_CH9120_Local_MASK[2],Recv_CH9120_Local_MASK[3]);
						
						break; 
					}						
				}
			}
			
			UART2_Rx_cnt = 0;
			for(n=0;n<4;n++)
			{
				CH9120_Local_IP[n] = Recv_CH9120_Local_IP[n]; 
				CH9120_Local_GW[n] = Recv_CH9120_Local_GW[n];
				CH9120_Local_MASK[n] = Recv_CH9120_Local_MASK[n];
			}
			CH9120_RESET();
			CH9120_Config_ON();
			
			CH9120_Set_Local_IP();
			CH9120_Set_Local_MASK();
			CH9120_Set_Local_GW();
			CH9120_Updata_EEPROM();
			CH9120_Active_Config();
			CH9120_Config_OFF();
			
			
			return 3;
			
		}
		else
		{
			SysPrintf("err02: recv error ip-gw-mask format\r\n");
			UART2_Rx_cnt = 0;
			return 0;
		}
	
	}
	
	else if(UART2_Rx_cnt>=33) 
		{
			if( (strstr(UART2_Rx_Buf,"cmd=setdestipport=")!=NULL) && (strstr(UART2_Rx_Buf,":")!=NULL) && (strstr(UART2_Rx_Buf,"AA\r\n")!=NULL))
			{
				SysPrintf("recv:%s\r\n",UART2_Rx_Buf);
				
				for(i=0;i<UART2_Rx_cnt-20;i++)
				{
					if( (UART2_Rx_Buf[i]=='p') && (UART2_Rx_Buf[i+1]=='p') && (UART2_Rx_Buf[i+2]=='o') && (UART2_Rx_Buf[i+3]=='r') && (UART2_Rx_Buf[i+4]=='t') && (UART2_Rx_Buf[i+5]=='=') )
					{
						for(j=i+6;j<UART2_Rx_cnt-3;j++)
						{
							if( UART2_Rx_Buf[j]==':')
							{
								ip_begin = i + 6;
								ip_end = j - 1;
								SysPrintf("ip_begin=%d,ip_end=%d\r\n",ip_begin,ip_end);
								break;
							}
						}
						if(j==UART2_Rx_cnt-3)
						{
							SysPrintf("local set ip format err\r\n");
							return 0;
						}
						
						for(j=ip_end+2;j<UART2_Rx_cnt-2;j++)
						{
							
							if( (UART2_Rx_Buf[j]=='A') && (UART2_Rx_Buf[j+1]=='A') )
							{
								port_begin = ip_end + 2;
								port_end = j - 1;
								SysPrintf("port_begin=%d,port_end=%d\r\n",port_begin,port_end);
								break;
							}
								
						}
						if(j==UART2_Rx_cnt-2)
						{
							SysPrintf("local set port format err\r\n");
							return 0;
						}
						
						for(k=ip_begin;k<port_end+3;k++)
						{
							Config_Data[k-ip_begin] = UART2_Rx_Buf[k];
						}
						SysPrintf("write:%s\r\n",Config_Data);
						SysPrintf("len=%d\r\n",k-ip_begin);
						Write_Config(Config_Data,k-ip_begin);
						UART2_Rx_cnt = 0;
						
						Feedback_Err_Cnt = 0; 
						return 5;
						
					}
					else
					{
						
						
						;
					}
				}
				return 0;	
			}
			else
			{
				UART2_Rx_cnt = 0;
				return 0;
			}
		}
	else if(UART2_Rx_cnt>=10) 
	{
		if( (strstr(UART2_Rx_Buf,"setdhcp=")!=NULL) && (strstr(UART2_Rx_Buf,"\r\n")!=NULL)) 
		{
			if(strstr(UART2_Rx_Buf,"setdhcp=1")!=NULL) 
			{
				CH9120_DHCP = CH9120_DHCP_ENABLE;
				CH9120_RESET();
				CH9120_Config_ON();
				CH9120_Set_DHCP(CH9120_DHCP);
				CH9120_Updata_EEPROM();
				CH9120_Active_Config();
				CH9120_Config_OFF();
				
			}
			else if(strstr(UART2_Rx_Buf,"setdhcp=0")!=NULL) 
			{
				CH9120_DHCP = CH9120_DHCP_DISABLE;
				CH9120_RESET();
				CH9120_Config_ON();
				CH9120_Set_DHCP(CH9120_DHCP);
				CH9120_Updata_EEPROM();
				CH9120_Active_Config();
				CH9120_Config_OFF();
				
			}
			else
			{
				;
			}
		}
	}
	
	else
	{
		;
		return 0;  
	}
	
	

}

void Write_Config_Localip(void)
{
	;
}

void Write_flash_Config_DHCP(void)
{
	;
}

uint8_t Wait_4G_Remote_Config_cmd(void)
{
	
	
	
	
	
	
	uint16_t i,j,k;
	uint16_t m,n;
	uint16_t ip_begin,ip_end,port_begin,port_end;
	recv_server_cmd = 0; 
	
	
	if(EC200T_Tx_Flag==0) 
	{
		if(LPUART1_Rx_cnt>=33) 
		{
			if( (strstr(LPUART1_Rx_Buf,"cmd=setdestipport=")!=NULL) && (strstr(LPUART1_Rx_Buf,":")!=NULL) && (strstr(LPUART1_Rx_Buf,"AA\r\n")!=NULL))
			{
				SysPrintf("recv:%s\r\n",LPUART1_Rx_Buf);
				
				for(i=0;i<LPUART1_Rx_cnt-20;i++)
				{
					if( (LPUART1_Rx_Buf[i]=='p') && (LPUART1_Rx_Buf[i+1]=='p') && (LPUART1_Rx_Buf[i+2]=='o') && (LPUART1_Rx_Buf[i+3]=='r') && (LPUART1_Rx_Buf[i+4]=='t') && (LPUART1_Rx_Buf[i+5]=='=') )
					{
						for(j=i+6;j<LPUART1_Rx_cnt-3;j++)
						{
							if( LPUART1_Rx_Buf[j]==':')
							{
								ip_begin = i + 6;
								ip_end = j - 1;
								SysPrintf("ip_begin=%d,ip_end=%d\r\n",ip_begin,ip_end);
								break;
							}
						}
						if(j==LPUART1_Rx_cnt-3)
						{
							SysPrintf("4G remote set ip format err\r\n");
							return 0;
						}
						
						for(j=ip_end+2;j<LPUART1_Rx_cnt-2;j++)
						{
							
							if( (LPUART1_Rx_Buf[j]=='A') && (LPUART1_Rx_Buf[j+1]=='A') )
							{
								port_begin = ip_end + 2;
								port_end = j - 1;
								SysPrintf("port_begin=%d,port_end=%d\r\n",port_begin,port_end);
								break;
							}
								
						}
						if(j==LPUART1_Rx_cnt-2)
						{
							SysPrintf("4G remote set port format err\r\n");
							return 0;
						}
						
						for(k=ip_begin;k<port_end+3;k++)
						{
							Config_Data[k-ip_begin] = LPUART1_Rx_Buf[k];
						}
						SysPrintf("write:%s\r\n",Config_Data);
						SysPrintf("len=%d\r\n",k-ip_begin);
						Write_Config(Config_Data,k-ip_begin);
						LPUART1_Rx_cnt = 0;
						
						Feedback_Err_Cnt = 0; 
						return 5;
						
					}
					else
					{
						
						
						;
					}
				}
				return 0;	
			}
			else
			{
				LPUART1_Rx_cnt = 0;
				return 0;
			}
		}
		
		
		
		else if(LPUART1_Rx_cnt>=23)
		{
	
	
			if( (strstr(LPUART1_Rx_Buf,"cmd=setch0")!=NULL) && (strstr(LPUART1_Rx_Buf,"delay")!=NULL) && (strstr(LPUART1_Rx_Buf,"\r\n")!=NULL)) 
			{
				for(i=0;i<LPUART1_Rx_cnt-14;i++)
				{
					if( (LPUART1_Rx_Buf[i]=='h') && (LPUART1_Rx_Buf[i+1]=='0') && (LPUART1_Rx_Buf[i+3]=='d') && (LPUART1_Rx_Buf[i+4]=='e') && (LPUART1_Rx_Buf[i+5]=='l') && (LPUART1_Rx_Buf[i+6]=='a') && (LPUART1_Rx_Buf[i+7]=='y') && (LPUART1_Rx_Buf[i+12]=='s') && (LPUART1_Rx_Buf[i+13]=='\r') && (LPUART1_Rx_Buf[i+14]=='\n') )
					{
						if( (LPUART1_Rx_Buf[i+8]>='0') && (LPUART1_Rx_Buf[i+8]<='9') && (LPUART1_Rx_Buf[i+9]>='0') && (LPUART1_Rx_Buf[i+9]<='9') && (LPUART1_Rx_Buf[i+10]>='0') && (LPUART1_Rx_Buf[i+10]<='9') && (LPUART1_Rx_Buf[i+11]>='0') && (LPUART1_Rx_Buf[i+11]<='9') )
						{
							Time_Delay_Tick = 1000*( 1000*(LPUART1_Rx_Buf[i+8]-'0') + 100*(LPUART1_Rx_Buf[i+9]-'0') + 10*(LPUART1_Rx_Buf[i+10]-'0') + 1*(LPUART1_Rx_Buf[i+11]-'0') );
							Delay_Rely_ascii = LPUART1_Rx_Buf[i+2];
							recv_server_cmd = 1;
							LPUART1_Rx_cnt = 0;
							Feedback_Err_Cnt = 0; 
							return 4;
						}
						else
						{
							LPUART1_Rx_cnt = 0;
							return 0;
						}
					}
				}
				return 0;	
			}
			else
			{
				LPUART1_Rx_cnt = 0;
				return 0;
			}
		}
		
		else if(LPUART1_Rx_cnt>=15) 
		{
	
	
			if((strstr(LPUART1_Rx_Buf,"cmd=getstatus\r\n")!=NULL))
			{
				recv_server_cmd = 1;
				LPUART1_Rx_cnt = 0;
				Feedback_Err_Cnt = 0; 
				return 1;
			}
			
			else if((strstr(LPUART1_Rx_Buf,"cmd=setonch0")!=NULL) && (strstr(LPUART1_Rx_Buf,"\r\n")!=NULL)) 
			{
				for(i=0;i<LPUART1_Rx_cnt-4;i++)
				{
					if( (LPUART1_Rx_Buf[i]=='h') && (LPUART1_Rx_Buf[i+1]=='0') && (LPUART1_Rx_Buf[i+3]=='\r') && (LPUART1_Rx_Buf[i+4]=='\n'))
					{
						Set_Rely_ascii = LPUART1_Rx_Buf[i+2];
						recv_server_cmd = 1;
						LPUART1_Rx_cnt = 0;
						Feedback_Err_Cnt = 0; 
						return 2;
					}
				}
				return 0;
			}
			
			else if( (strstr(LPUART1_Rx_Buf,"cmd=setofch0")!=NULL) && (strstr(LPUART1_Rx_Buf,"\r\n")!=NULL)) 
			{
				for(i=0;i<LPUART1_Rx_cnt-4;i++)
				{
					if( (LPUART1_Rx_Buf[i]=='h') && (LPUART1_Rx_Buf[i+1]=='0') && (LPUART1_Rx_Buf[i+3]=='\r') && (LPUART1_Rx_Buf[i+4]=='\n'))
					{
						Reset_Rely_ascii = LPUART1_Rx_Buf[i+2];
						recv_server_cmd = 1;
						LPUART1_Rx_cnt = 0;
						Feedback_Err_Cnt = 0; 
						return 3;
					}
				}
				return 0;			
			}

			else
			{
				LPUART1_Rx_cnt = 0;
				return 0;
			}

		}
	
	
		else
		{
			;
			return 0;  
		}
	}
	
	else
	{
		
		if(LPUART1_Rx_cnt>=2)
		{
			if(strstr(LPUART1_Rx_Buf,"++")!=NULL)
			{
				Feedback_Err_Cnt = 0;
				for(m=0;m<LPUART1_Rx_cnt-1;m++)
				{
					if((LPUART1_Rx_Buf[m]=='+') && (LPUART1_Rx_Buf[m+1]=='+'))
					{
						LPUART1_Rx_Buf[m] = 0;
						LPUART1_Rx_Buf[m+1] = 0;
						for(n=m;n<LPUART1_Rx_cnt-2;n++)
						{
							LPUART1_Rx_Buf[n] = LPUART1_Rx_Buf[n+2];
						}
						LPUART1_Rx_cnt = LPUART1_Rx_cnt - 2;
						break;
					}
				}
			}
			else
			{
				Feedback_Err_Cnt++;
			}

			return 7;
		}
		else
		{
			Feedback_Err_Cnt++;
			return 8;
		}
	
	
	}
}

uint8_t Wait_CH9120_Remote_Config_cmd(void)
{
	
	
	
	
	
	uint16_t i,j,k;
	uint16_t m,n;
	uint16_t ip_begin,ip_end,port_begin,port_end;
	recv_server_cmd = 0; 
	
	if(Eth0_Tx_Flag==0)
	{
		if(UART1_Rx_cnt>=33) 
		{
			if( (strstr(UART1_Rx_Buf,"cmd=setdestipport=")!=NULL) && (strstr(UART1_Rx_Buf,":")!=NULL) && (strstr(UART1_Rx_Buf,"AA\r\n")!=NULL))
			{
				SysPrintf("recv:%s\r\n",UART1_Rx_Buf);
				
				for(i=0;i<UART1_Rx_cnt-20;i++)
				{
					if( (UART1_Rx_Buf[i]=='p') && (UART1_Rx_Buf[i+1]=='p') && (UART1_Rx_Buf[i+2]=='o') && (UART1_Rx_Buf[i+3]=='r') && (UART1_Rx_Buf[i+4]=='t') && (UART1_Rx_Buf[i+5]=='=') )
					{
						for(j=i+6;j<UART1_Rx_cnt-3;j++)
						{
							if( UART1_Rx_Buf[j]==':')
							{
								ip_begin = i + 6;
								ip_end = j - 1;
								SysPrintf("ip_begin=%d,ip_end=%d\r\n",ip_begin,ip_end);
								break;
							}
						}
						if(j==UART1_Rx_cnt-3)
						{
							SysPrintf("eth0 remote set ip format err\r\n");
							return 0;
						}
						
						for(j=ip_end+2;j<UART1_Rx_cnt-2;j++)
						{
							
							if( (UART1_Rx_Buf[j]=='A') && (UART1_Rx_Buf[j+1]=='A') )
							{
								port_begin = ip_end + 2;
								port_end = j - 1;
								SysPrintf("port_begin=%d,port_end=%d\r\n",port_begin,port_end);
								break;
							}
								
						}
						if(j==UART1_Rx_cnt-2)
						{
							SysPrintf("eth0 remote set port format err\r\n");
							return 0;
						}
						
						for(k=ip_begin;k<port_end+3;k++)
						{
							Config_Data[k-ip_begin] = UART1_Rx_Buf[k];
						}
						SysPrintf("write:%s\r\n",Config_Data);
						SysPrintf("len=%d\r\n",k-ip_begin);
						Write_Config(Config_Data,k-ip_begin);
						UART1_Rx_cnt = 0;
						Feedback_Err_Cnt = 0; 
						return 5;
						
					}
					else
					{
						
						
						;
					}
				}
				return 0;	
			}
			else
			{
				UART1_Rx_cnt = 0;
				return 0;
			}
		}
		
		else if(UART1_Rx_cnt>=23)
		{
			if( (strstr(UART1_Rx_Buf,"cmd=setch0")!=NULL) && (strstr(UART1_Rx_Buf,"delay")!=NULL) && (strstr(UART1_Rx_Buf,"\r\n")!=NULL)) 
			{
				for(i=0;i<UART1_Rx_cnt-14;i++)
				{
					if( (UART1_Rx_Buf[i]=='h') && (UART1_Rx_Buf[i+1]=='0') && (UART1_Rx_Buf[i+3]=='d') && (UART1_Rx_Buf[i+4]=='e') && (UART1_Rx_Buf[i+5]=='l') && (UART1_Rx_Buf[i+6]=='a') && (UART1_Rx_Buf[i+7]=='y') && (UART1_Rx_Buf[i+12]=='s') && (UART1_Rx_Buf[i+13]=='\r') && (UART1_Rx_Buf[i+14]=='\n') )
					{
						if( (UART1_Rx_Buf[i+8]>='0') && (UART1_Rx_Buf[i+8]<='9') && (UART1_Rx_Buf[i+9]>='0') && (UART1_Rx_Buf[i+9]<='9') && (UART1_Rx_Buf[i+10]>='0') && (UART1_Rx_Buf[i+10]<='9') && (UART1_Rx_Buf[i+11]>='0') && (UART1_Rx_Buf[i+11]<='9') )
						{
							Time_Delay_Tick = 1000*( 1000*(UART1_Rx_Buf[i+8]-'0') + 100*(UART1_Rx_Buf[i+9]-'0') + 10*(UART1_Rx_Buf[i+10]-'0') + 1*(UART1_Rx_Buf[i+11]-'0') );
							Delay_Rely_ascii = UART1_Rx_Buf[i+2];
							recv_server_cmd = 1;
							UART1_Rx_cnt = 0;
							Feedback_Err_Cnt = 0; 
							return 4;
						}
						else
						{
							UART1_Rx_cnt = 0;
							return 0;
						}
					}
				}
				return 0;	
			}
			else
			{
				UART1_Rx_cnt = 0;
				return 0;
			}
		}
		
		else if(UART1_Rx_cnt>=15) 
		{
			
			
			if((strstr(UART1_Rx_Buf,"cmd=getstatus\r\n")!=NULL))
			{
				
				recv_server_cmd = 1;
				UART1_Rx_cnt = 0;
				Feedback_Err_Cnt = 0; 
				return 1;
			}
			
			else if((strstr(UART1_Rx_Buf,"cmd=setonch0")!=NULL) && (strstr(UART1_Rx_Buf,"\r\n")!=NULL)) 
			{
				for(i=0;i<UART1_Rx_cnt-4;i++)
				{
					if( (UART1_Rx_Buf[i]=='h') && (UART1_Rx_Buf[i+1]=='0') && (UART1_Rx_Buf[i+3]=='\r') && (UART1_Rx_Buf[i+4]=='\n'))
					{
						Set_Rely_ascii = UART1_Rx_Buf[i+2];
						recv_server_cmd = 1;
						UART1_Rx_cnt = 0;
						Feedback_Err_Cnt = 0; 
						return 2;
					}
				}
				return 0;
			}
			
			else if( (strstr(UART1_Rx_Buf,"cmd=setofch0")!=NULL) && (strstr(UART1_Rx_Buf,"\r\n")!=NULL)) 
			{
				for(i=0;i<UART1_Rx_cnt-4;i++)
				{
					if( (UART1_Rx_Buf[i]=='h') && (UART1_Rx_Buf[i+1]=='0') && (UART1_Rx_Buf[i+3]=='\r') && (UART1_Rx_Buf[i+4]=='\n'))
					{
						Reset_Rely_ascii = UART1_Rx_Buf[i+2];
						recv_server_cmd = 1;
						UART1_Rx_cnt = 0;
						Feedback_Err_Cnt = 0; 
						return 3;
					}
				}
				return 0;			
			}

			else
			{
				UART1_Rx_cnt = 0;
				return 0;
			}
		}
		else
		{
			;
			
			
			return 0;  
			
			
		}
	}
	else
	{
		
		if(UART1_Rx_cnt>=2)
		{
			if(strstr(UART1_Rx_Buf,"++")!=NULL)
			{
				Feedback_Err_Cnt = 0;
				for(m=0;m<UART1_Rx_cnt-1;m++)
				{
					if((UART1_Rx_Buf[m]=='+') && (UART1_Rx_Buf[m+1]=='+'))
					{
						UART1_Rx_Buf[m] = 0;
						UART1_Rx_Buf[m+1] = 0;
						for(n=m;n<UART1_Rx_cnt-2;n++)
						{
							UART1_Rx_Buf[n] = UART1_Rx_Buf[n+2];
						}
						UART1_Rx_cnt = UART1_Rx_cnt - 2;
						break;
					}
				}
			}
			else
			{
				Feedback_Err_Cnt++;
			}

			return 7;
		}
		else
		{
			Feedback_Err_Cnt++;
			return 8;
		}
	
	
	}
}





void SystemClock_Config(void);









/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  
	
	uint16_t i;
	uint8_t MCU_IO_IN_Val_bf = 0x00;
	uint8_t status_change = 0x00;
	uint8_t peiod_upload = 0x00;
	uint8_t keep_link_upload = 0x00;
	uint8_t reboot_upload = 0x01;
	uint8_t Config_4G_relt = 0x00;
	uint8_t Config_CH9120_relt = 0x00;
	uint8_t Get_Data_upload = 0;
	uint8_t EC200T_Init_ret;
	uint8_t CH9120_Link_ret;
	float temp = 0.0;
	uint8_t retry_cnt = 3;
  

  

  
  HAL_Init();

  

  

  
  SystemClock_Config();

  

  

  
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_LPUART1_UART_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_RTC_Init();
  MX_TIM6_Init();
  






	
	SysPrintf("%s\r\n",VERSION);
	read_sys_config();
	if(Read_Config()==0)
	{
		SysPrintf("read err,sys will use default config-info\r\n");
	}
	else
	{
		SysPrintf("read ok,sys will use user config-info\r\n");
	}
	LEDNET_OFF;
	HAL_UART_Receive_IT(&hlpuart1,(uint8_t *)LPUART1_temp,REC_LENGTH); 
	HAL_UART_Receive_IT(&huart1,(uint8_t *)UART1_temp,REC_LENGTH); 
	
/*
	while(1)
	{
		adc_channel0_status_bf = adc_channel0_status;
		adc_channel1_status_bf = adc_channel1_status;
		adc_channel0_val=Get_Adc_Average(ADC_CHANNEL_0,adc_sample_time); 
		SysPrintf("adc0=%d\r\n",adc_channel0_val);
		HAL_Delay(1000);
	}
	*/
		
		
		SysPrintf("wait local config>>>>>>>\r\n");	
		HAL_UART_Receive_IT(&huart2,(uint8_t *)UART2_temp,REC_LENGTH); 
		HAL_Delay(3000);
		Wait_Local_Config_cmd();
		
		Poll_LED(6);
		
		
		
		{
			CH9120_Init(CH9120_Mode); 
		}
		
		Period_Tick = 0;
		Keep_link_Tick = 0;
		
		CH9120_Read_Config();
		SysPrintf("check eth0 link status...\r\n");
		CH9120_Link_ret = CH9120_Check_Link_Status();
		UART1_Rx_cnt = 0;
		LPUART1_Rx_cnt = 0;
	
	
	PW_ON_EC200T();
		

	
	
	
		
	
	
	
		
	EC200T_Init_ret = EC200T_Init();
	while(retry_cnt--)
	{
		if(EC200T_Init_ret==1) 
			break;
		else
		{
			SysPrintf("check eth0 link status...\r\n");
			CH9120_Link_ret = CH9120_Check_Link_Status();
			UART1_Rx_cnt = 0;
			if(CH9120_Link_ret==1)
			{
				break;
			}
			else
			{
				SysPrintf("eth0 link error, 4G tcp link err and re-Init...\r\n");
					
				
				
				
				
			
				
				
				EC200T_Init_ret = EC200T_Init();
				
				SysPrintf("reboot CH9120 retry to link server...\r\n");
				CH9120_RESET();
				
				




				
				
				SysPrintf("waiting CH9120 link server...\r\n");
				HAL_Delay(1000);
				SysPrintf("waiting CH9120 link server...\r\n");
				HAL_Delay(1000);
				SysPrintf("waiting CH9120 link server...\r\n");
				HAL_Delay(1000);
				CH9120_Link_ret = CH9120_Check_Link_Status();
				UART1_Rx_cnt = 0;
				if(CH9120_Link_ret==1)
				{
					break;
				}
				
			}
		}
	}
	
	if(EC200T_Init_ret==1) LEDNET_ON;
	
	while(1)
	{
		
		if( ((Tx_Err_Cnt>=4) || (Feedback_Err_Cnt>=12)) && (CH9120_Link_ret==0) ) 
		{
			LEDNET_OFF;
			Tx_Err_Cnt = 0;
			Feedback_Err_Cnt = 0;
			SysPrintf("eth0 link err and 4G tcp sent err, relink server...\r\n");
			
			
			
			
			PW_ON_EC200T();
			EC200T_Init();			
			CH9120_Link_ret = CH9120_Check_Link_Status();
			
			
		}

		if(HAL_GetTick()>Reboot_Time_Period) 
		{
			NVIC_SystemReset();
		}
		
		if(!Period_Tick)  
			Period_Tick = HAL_GetTick();
		if((HAL_GetTick() - Period_Tick)>=Time_Period) 
		{
			Period_Tick = 0;
			peiod_upload = 1;
		}
		else
		{
			peiod_upload = 0;
		}
		
		
		if(!Keep_link_Tick)
			Keep_link_Tick = HAL_GetTick();
		if((HAL_GetTick() - Keep_link_Tick)>=Keep_Link_Period) 
		{
			Keep_link_Tick = 0;
			keep_link_upload = 1;
		}
		else
		{
			keep_link_upload = 0;
		}
		
		if(peiod_upload==1) 
		{
			keep_link_upload = 0;
		}
		
		
		
		

		 if(!Sample_adc_Tick)
			Sample_adc_Tick = HAL_GetTick();
		
		if( ((HAL_GetTick() - Sample_adc_Tick)>=Sample_adc_Period) || (peiod_upload==1) ) 
		{
			
			Sample_adc_Tick = 0;
			adc_channel0_status_bf = adc_channel0_status;
			adc_channel1_status_bf = adc_channel1_status;
			adc_channel0_val=Get_Adc_Average(ADC_CHANNEL_0,adc_sample_time); 
			adc_channel0_val = adc_channel0_val*33000/4096;
			SysPrintf("adc0=%duA\r\n",adc_channel0_val);
			
			adc_channel1_val=Get_Adc_Average(ADC_CHANNEL_1,adc_sample_time); 
			adc_channel1_val = adc_channel1_val*33000/4096;
			SysPrintf("adc1=%duA\r\n",adc_channel1_val);
			
			if((adc_channel0_val<adc_channel0_low_th) && (adc_channel0_status_bf==0))
			{
				adc_channel0_status = 1;
				status_change = 1;
				SysPrintf("adc0 changed...\r\n");
			}
			else if((adc_channel0_val>adc_channel0_high_th) && (adc_channel0_status_bf==1))
			{
				adc_channel0_status = 0;
				status_change = 1;
				SysPrintf("adc0 changed...\r\n");
			}
			else 
			{
				adc_channel0_status = adc_channel0_status;
				status_change = status_change;
			}
			
			if((adc_channel1_val<adc_channel1_low_th) && (adc_channel1_status_bf==0))
			{
				adc_channel1_status = 1;
				status_change = 1;
				SysPrintf("adc1 changed...\r\n");
			}
			else if((adc_channel1_val>adc_channel1_high_th) && (adc_channel1_status_bf==1))
			{
				adc_channel1_status = 0;
				status_change = 1;
				SysPrintf("adc1 changed...\r\n");
			}
			else 
			{
				adc_channel0_status = adc_channel0_status;
				status_change = status_change;
			}
			
			
			
			
			
			backup_pwr_bf = backup_pwr;
			adc_channel5_val=Get_Adc_Average(ADC_CHANNEL_5,adc_sample_time);
			temp = (( (float)(adc_channel5_val) )*7.52);
			adc_channel5_val = (uint16_t)temp;
			if(adc_channel5_val < 3000)  adc_channel5_val = 0;
			
			SysPrintf("battary_vol=%dmv\r\n",adc_channel5_val);
			
			if((adc_channel5_val>adc_channel5_high_th) && (backup_pwr_bf==0))
			{
				backup_pwr = 1;
				status_change = 1;
				SysPrintf("power changed...\r\n");
			}
			else if((adc_channel5_val<adc_channel5_low_th) && (backup_pwr_bf==1))
			{
				backup_pwr = 0;
				status_change = 1;
				SysPrintf("power changed...\r\n");
			}
			else
			{
				backup_pwr = backup_pwr;
				status_change = status_change;
			}
		
		
		
		}
		
		else
		{
			;	
		} 
		
		
		
		
		

		
		MCU_IO_IN_Val_bf = MCU_IO_IN_Val;
		GetIo_Level();
		if(MCU_IO_IN_Val_bf!=MCU_IO_IN_Val)
		{
			status_change = 1;
			SysPrintf("IO-in changed...\r\n");
		}
		
		
		
		
		if( (Rely1_status_bf!=Rely1_status) || (Rely2_status_bf!=Rely2_status) || (Rely3_status_bf!=Rely3_status) ||(Rely4_status_bf!=Rely4_status) )
		{
			status_change = 1;
			SysPrintf("RELY changed...\r\n");
		}
		Rely1_status_bf = Rely1_status;
		Rely2_status_bf = Rely2_status;
		Rely3_status_bf = Rely3_status;
		Rely4_status_bf = Rely4_status;
		
		
		
		
		
		
		
		
		Config_4G_relt = Wait_4G_Remote_Config_cmd();		
		
		/*
		if( (Config_4G_relt==1) || (Config_4G_relt==2) || (Config_4G_relt==3) || (Config_4G_relt==4)) 
		{
			
			
			SysPrintf("4G sent ack packet\r\n");
			Pack_cnt++;
			Keep_Link_Packet[8] = '3';
			Keep_Link_Packet[33] = lowhextoascii(Pack_cnt>>4);
			Keep_Link_Packet[34] = lowhextoascii(Pack_cnt);
			EC200T_Sent_Data(Keep_Link_Packet,39);
			
		}
		*/
		if(Config_4G_relt==1)
		{
			SysPrintf("4G upload data...\r\n");
			Get_Data_upload = 1;
		}
		else if(Config_4G_relt==2)
		{
			SysPrintf("4G on rely...\r\n");
			Set_Rely(Set_Rely_ascii);
		}
		else if(Config_4G_relt==3)
		{
			SysPrintf("4G off rely...\r\n");
			Reset_Rely(Reset_Rely_ascii);
		}
		else if(Config_4G_relt==4)
		{
			SysPrintf("4G on-delay-off rely...\r\n");
			Delay_Rely(Delay_Rely_ascii);
		}
		else
		{
			
		}
		
		Config_4G_relt = 0;
		
		if(Rely1_Delay_flag==1)
		{ 
			if(!Rely1_Delay_Tick)  Rely1_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely1_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x01);
			}
		}
		if(Rely2_Delay_flag==1)
		{ 
			if(!Rely2_Delay_Tick)  Rely2_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely2_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x02);
			}
		}
		if(Rely3_Delay_flag==1)
		{ 
			if(!Rely3_Delay_Tick)  Rely3_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely3_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x04);
			}
		}
		if(Rely4_Delay_flag==1)
		{ 
			if(!Rely4_Delay_Tick)  Rely4_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely4_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x08);
			}
		}
		
		
		
		
		
		
		Config_CH9120_relt = Wait_CH9120_Remote_Config_cmd();
		
		/*
		if( (Config_CH9120_relt==1) || (Config_CH9120_relt==2) || (Config_CH9120_relt==3) || (Config_CH9120_relt==4)) 
		{
			SysPrintf("eth0 sent ack packet\r\n");
			Pack_cnt++;
			Keep_Link_Packet[8] = '3';
			Keep_Link_Packet[33] = lowhextoascii(Pack_cnt>>4);
			Keep_Link_Packet[34] = lowhextoascii(Pack_cnt);
			
			CH9120_SentData(Keep_Link_Packet,39);
		}
		*/
		if(Config_CH9120_relt==1)
		{
			SysPrintf("CH9120 upload data...\r\n");
			Get_Data_upload = 1;
		}
		else if(Config_CH9120_relt==2)
		{
			SysPrintf("CH9120 on rely...\r\n");
			Set_Rely(Set_Rely_ascii);
		}
		else if(Config_CH9120_relt==3)
		{
			SysPrintf("CH9120 off rely...\r\n");
			Reset_Rely(Reset_Rely_ascii);
		}
		else if(Config_CH9120_relt==4)
		{
			SysPrintf("CH9120 on-delay-off rely...\r\n");
			Delay_Rely(Delay_Rely_ascii);
		}
		else
		{
			
		}
		
		
		Config_CH9120_relt = 0;
		
		if(Rely1_Delay_flag==1)
		{ 
			if(!Rely1_Delay_Tick)  Rely1_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely1_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x01);
			}
		}
		if(Rely2_Delay_flag==1)
		{ 
			if(!Rely2_Delay_Tick)  Rely2_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely2_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x02);
			}
		}
		if(Rely3_Delay_flag==1)
		{ 
			if(!Rely3_Delay_Tick)  Rely3_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely3_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x04);
			}
		}
		if(Rely4_Delay_flag==1)
		{ 
			if(!Rely4_Delay_Tick)  Rely4_Delay_Tick = HAL_GetTick();
			if((HAL_GetTick() - Rely4_Delay_Tick)>=Time_Delay_Tick)
			{
				Reset_Rely(0x08);
			}
		}
		
		
		
		
		
		
		EC200T_Tx_Flag = 0;
		Eth0_Tx_Flag = 0;
		
		if((status_change==1) || (peiod_upload==1) || (reboot_upload==1) || (Get_Data_upload==1) || (keep_link_upload==1))
		{
			
			
			
			SysPrintf("Feedback_Err_Cnt=%d\r\n",Feedback_Err_Cnt);
			if(status_change==1)
			{
				Pack_cnt++;
				SysPrintf("sent remote control device active packet len=82...\r\n");
				HeartBeat_Packet[8] = '2';
				Pack_packet();
				SysPrintf("%s\r\n",HeartBeat_Packet);
				EC200T_Sent_Data(HeartBeat_Packet,82);
				CH9120_SentData(HeartBeat_Packet,82);
				EC200T_Tx_Flag = 1;
				Eth0_Tx_Flag = 1;
			}
			else if((peiod_upload==1) || (reboot_upload==1) || (Get_Data_upload==1))
			{
				if(peiod_upload==1)
					SysPrintf("period type...\r\n");
				if(reboot_upload==1)
					SysPrintf("reboot type...\r\n");
				if(Get_Data_upload==1)
					SysPrintf("upload type...\r\n");
				Pack_cnt++;
				SysPrintf("sent heart beat packet len=82...\r\n");
				HeartBeat_Packet[8] = '1';
				Pack_packet();
				SysPrintf("%s\r\n",HeartBeat_Packet);
				EC200T_Sent_Data(HeartBeat_Packet,82);
				CH9120_SentData(HeartBeat_Packet,82);
				EC200T_Tx_Flag = 1;
				Eth0_Tx_Flag = 1;
			}
			else if(keep_link_upload==1)
			{
				SysPrintf("sent keep link packet len=39...\r\n");
				HeartBeat_Packet[8] = '0';
				Keep_Link_Packet[8] = '0';
				Keep_Link_Packet[33] = lowhextoascii(Pack_cnt>>4);
				Keep_Link_Packet[34] = lowhextoascii(Pack_cnt);
				Keep_Link_Packet[35] = 'A';
				Keep_Link_Packet[36] = 'A';
				Keep_Link_Packet[37] = 'A';
				Keep_Link_Packet[38] = 'A';
				SysPrintf("%s\r\n",Keep_Link_Packet);
				EC200T_Sent_Data(Keep_Link_Packet,39);
				CH9120_SentData(Keep_Link_Packet,39);
				EC200T_Tx_Flag = 1;
				Eth0_Tx_Flag = 1;
			}
			else
			{
				;
			}

		}
		
		HAL_Delay(30);
		status_change = 0;
		peiod_upload = 0;
		reboot_upload = 0;
		Get_Data_upload = 0;
		keep_link_upload = 0;
		
		
		
		
		if(Pack_cnt==100)
		{
			Sent_Pack_Tick = HAL_GetTick();
		}
		
		if(Pack_cnt==110)
		{
			if( (HAL_GetTick()- Sent_Pack_Tick)<= 20000 ) 
			{
				SysPrintf("sys err, sys rebooting..\r\n");
				NVIC_SystemReset();
			}
		}
		
		
		
		
	}
	
	
  

  
  
  while (1)
  {
    

    
  }
  
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_RTC;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}





/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  
  
  __disable_irq();
  while (1)
  {
  }
  
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
}
#endif 


